package com.example.sunmiv1.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sunmiv1.R;
import com.example.sunmiv1.utils.net.ACache;
import com.example.sunmiv1.utils.net.LoadingDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChooseActivity extends Activity {

    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.title_title)
    TextView titleTitle;
    @BindView(R.id.collect_collect)
    TextView collectCollect;
    @BindView(R.id.collect_refund)
    TextView collectRefund;
    @BindView(R.id.collect_record)
    TextView collectRecord;

    private ACache aCache;
    private Context context;
    private Intent intent;
    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_choose);
        context = this;
        aCache = ACache.get(context);
        ButterKnife.bind(this);
        loadingDialog = new LoadingDialog(context);
        titleBack.setVisibility(View.VISIBLE);
        titleTitle.setText(context.getResources().getText(R.string.main_collect));
        initData();
    }

    private void initData() {
        startQrCode();
    }

    private void startQrCode() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            // 申請權限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 11003);
            return;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 11003:
                // 攝像頭權限申請
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // 獲得授權
                    startQrCode();
                } else {
                    // 被禁止授權
                    startQrCode();
                    Toast.makeText(this, "請至權限中心打開本應用的相機訪問權限", Toast.LENGTH_LONG).show();
                }
                break;
        }
    }

    @OnClick({R.id.title_back, R.id.collect_collect, R.id.collect_refund,R.id.collect_record})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.collect_collect:
                intent = new Intent(context, CollectActivity.class);
                startActivity(intent);
                break;
            case R.id.collect_refund:
                intent = new Intent(context, RefundActivity.class);
                startActivity(intent);
                break;
            case R.id.collect_record:
                intent = new Intent(context, CollectRecordActivity.class);
                startActivity(intent);
                break;
        }
    }
}
